package com.dtf.admin.manage.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.dtf.admin.common.utils.MapUtils;
import com.dtf.admin.common.utils.mybatis.DaoUtils;
import com.dtf.admin.manage.service.SysStaffBo;
import com.dtf.admin.manage.vo.SysStaff;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service(version = "1.0.0",interfaceClass=SysStaffBo.class)
public class SysStaffBoImpl implements SysStaffBo{
	
	private static final Log log = LogFactory.getLog(SysStaffBoImpl.class);
	
	@Autowired
	private SqlSessionTemplate sessionTemplate;
	
	@Autowired
	private DaoUtils daoUtils;
	
	public PageInfo querySysStaff(int pageNum, int pageSize){
		return daoUtils.selectPage("SysStaff.querySysStaff", pageNum,pageSize);
	}
	
	public SysStaff findSysStaffVo(Map param){
		return daoUtils.selectOne("SysStaff.findSysStaffVo", param);
	}
	
	public Map findSysStaffMap(Map param){
		return daoUtils.selectOne("SysStaff.findSysStaffMap", param);
	}
	
	@Transactional   //添加事务
	public Map updateSysStaffName(Map param){
		Map staff = daoUtils.selectOne("SysStaff.findSysStaffMap", param);
		staff.put("staff_name", MapUtils.getString(param, "staff_name"));
		daoUtils.update("updateSysStaff",staff);
		return daoUtils.selectOne("SysStaff.findSysStaffMap", param);
	}
	
	/***
	 * 测试事务控制，当修改条数成功后，抛出异常，看会不会回滚
	 * @param param
	 * @return
	 */
	@Transactional
	public int updateSysStaffError(Map param){
		Map staff = daoUtils.selectOne("SysStaff.findSysStaffMap", param);
		staff.put("staff_name", MapUtils.getString(param, "staff_name"));
		int c = daoUtils.update("updateSysStaff",param);
		log.info("变更条数："+c);
		if (true) {
			throw new RuntimeException("插入发生错误，回滚！");
		}
		return c;
	}
	
	/**
	 * 直接通过sessionTemplate操作数据库
	 * @return
	 */
	public List get() {
		PageHelper.startPage(1, 2);
		return sessionTemplate.selectList("CustMapper.findCust");
	}
	
}
